package com.eairlv.route.client.core;

import feign.Client;
import feign.Request;
import feign.Response;
import org.springframework.cloud.netflix.feign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;

import java.io.IOException;

/**
 * @author eairlv
 * @description
 * @date 10:17 2019/12/10
 */
public class RouteLoadBalancerFeignClient extends LoadBalancerFeignClient {

    public RouteLoadBalancerFeignClient(Client delegate,
                                        CachingSpringLoadBalancerFactory lbClientFactory,
                                        SpringClientFactory clientFactory) {
        super(delegate, lbClientFactory, clientFactory);
    }

    /**
     * 重写请求方法
     * @param request
     * @param options
     * @return
     * @throws IOException
     */
    @Override
    public Response execute(Request request, Request.Options options) throws IOException {
        if (RouteInvocationHandler.route_header.get() != null){
            return new RouteClient(null, null).execute(request, options);
        } else {
            return super.execute(request, options);
        }
    }
}
